<template>
  <div class="systemSetting-container registerInfo-container">
    <el-tabs v-model="activeName" @tab-click="handleClick" class="el-tab-pane">
      <el-tab-pane label="系统设置" name="first">
        <div class="table">
          <el-table
            :data="tableData"
            border
            :header-cell-style="{background:'#F5F5F5',color:'#000000',textAlign:'center'}"
          >
            <el-table-column
              prop="module"
              align="center"
              label="模块"
              min-width="180"
            >
<!--              <template slot-scope="scope">-->

<!--              </template>-->
            </el-table-column>
            <el-table-column
              align="left"
              label="功能说明"
              min-width="400"
            >
              <template slot-scope="scope">
                <div v-if="scope.row.module=='挂号管理'">
                  <span>开通挂号功能时，可使用门诊挂号功能、将病人分诊到医生、 设置挂号费用、统计中计入挂号费。</span><br>
                  <span style="color:#3E909E">注意：请勿在其他人登系统时，进行切换操作。</span>
                </div>
                <div v-if="scope.row.module=='药品管理'">
                  <span>功能说明</span><br>
                  <span style="color:#3E909E">1、切换是否使用批次管理前，请阅读功能说明，请勿随意切换。</span><br>
                  <span style="color:#3E909E">2、请勿在其他人登系统时，进行切换操作。</span>
                </div>
                <div v-if="scope.row.module=='收费/发药管理'">
                  <span>开通收费/发药功能时，收费后将立即发药，将省去单独发药 操作；关闭此功能时，将分开操作。</span><br>
                  <span style="color:#3E909E">注意：请勿在其他人登系统时，进行切换操作。</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="setting"
              align="center"
              label="功能设置"
              min-width="400"
            >
              <template slot-scope="scope">
                <span v-if="scope.row.module=='挂号管理'">是否使用挂号管理</span>
                <span v-if="scope.row.module=='药品管理'">是否使用药品管理</span>
                <span v-if="scope.row.module=='收费/发药管理'">是否使用收费/发药管理</span>
                <template v-if="scope.row.module=='收费/发药管理'">
                  <el-radio v-model="scope.row.setting" :label="false">是</el-radio>
                  <el-radio v-model="scope.row.setting" :label="true">否</el-radio>
                </template>
                <template v-else>
                  <el-radio v-model="scope.row.setting" :label="true">是</el-radio>
                  <el-radio v-model="scope.row.setting" :label="false">否</el-radio>
                </template>

                <el-button class="border-btn" @click="saveSetting()">保存设置</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="block">
<!--            <el-pagination-->
<!--              @size-change="handleSizeChange"-->
<!--              @current-change="handleCurrentChange"-->
<!--              :current-page="currentPage"-->
<!--              :page-sizes="[100, 200, 300, 400]"-->
<!--              :page-size="100"-->
<!--              layout="total, sizes, prev, pager, next, jumper"-->
<!--              :total="400">-->
<!--            </el-pagination>-->
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="打印设置" name="second">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <div class="up">
          <div class="up-title"><span>小票设置</span></div>
          <div class="up-content">
            <el-col :span="12">
              <el-form-item label="纸张" prop="size">
                <el-col :span="12">
                  <el-select
                    style="width: 100%"
                    v-model="ruleForm.size"
                    filterable
                    allow-create
                    default-first-option
                    placeholder="请选择纸张大小">
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-col>
              </el-form-item>
              <el-form-item label="抬头" prop="look">
                <el-col :span="12">
                  <el-input v-model="ruleForm.look"></el-input>
                </el-col>
              </el-form-item>
              <el-form-item label="落款" prop="footer">
                <el-col :span="12">
                  <el-input v-model="ruleForm.footer"></el-input>
                </el-col>
              </el-form-item>
              <el-form-item label="打印明细" prop="des">
                <el-col :span="12">
                  <div class="selectSex">
                    <div class="sex">
                      <el-radio v-model="ruleForm.des" :label="true" border align="center">是</el-radio>
                    </div>
                    <div class="sex">
                      <el-radio v-model="ruleForm.des" :label="false" border align="center" >否</el-radio>
                    </div>
                  </div>
                </el-col>
              </el-form-item>
            </el-col>
          </div>
        </div>
        <div class="down">
          <div class="up-title"><span>处方设置</span></div>
          <div class="up-content">
            <el-col :span="12">
              <el-form-item label="抬头" prop="lookc">
                <el-col :span="12">
                  <el-input v-model="ruleForm.lookc"></el-input>
<!--                  <el-select-->
<!--                    style="width: 100%"-->
<!--                    v-model="ruleForm.look"-->
<!--                    filterable-->
<!--                    allow-create-->
<!--                    default-first-option-->
<!--                    placeholder="请选择抬头">-->
<!--                    <el-option-->
<!--                      v-for="item in options"-->
<!--                      :key="item.value"-->
<!--                      :label="item.label"-->
<!--                      :value="item.value">-->
<!--                    </el-option>-->
<!--                  </el-select>-->
                </el-col>
              </el-form-item>
              <el-form-item label="处方方向" prop="direction">
                <el-col :span="12">
                  <div class="selectSex">
                    <div class="sex">
                      <el-radio v-model="ruleForm.direction" label="1" border align="center">横版</el-radio>
                    </div>
                    <div class="sex">
                      <el-radio v-model="ruleForm.direction" label="2" border align="center" >竖版</el-radio>
                    </div>
                  </div>
                </el-col>
              </el-form-item>
              <el-form-item label="打印单价" prop="price">
                <el-col :span="12">
                  <div class="selectSex">
                    <div class="sex">
                      <el-radio v-model="ruleForm.price" :label="true" border align="center">是</el-radio>
                    </div>
                    <div class="sex">
                      <el-radio v-model="ruleForm.price" :label="false" border align="center" >否</el-radio>
                    </div>
                  </div>
                </el-col>
              </el-form-item>
              <el-form-item label="打印总价" prop="total">
                <el-col :span="12">
                  <div class="selectSex">
                    <div class="sex">
                      <el-radio v-model="ruleForm.total" :label="true" border align="center">是</el-radio>
                    </div>
                    <div class="sex">
                      <el-radio v-model="ruleForm.total" :label="false" border align="center" >否</el-radio>
                    </div>
                  </div>
                </el-col>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="打印样式示例" prop="example">
                <img src="../../../../public/1.png">
              </el-form-item>
            </el-col>
          </div>
        </div>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <el-button size='mini' class="save" v-if="activeName=='second'" @click="savePrint()">保存</el-button>
<!--    <el-dialog-->
<!--      class="dialog"-->
<!--      title="系统提示"-->
<!--      :visible.sync="dialogVisible"-->
<!--      width="24%"-->
<!--      :close-on-click-modal="false">-->
<!--      <div class="div_title">是否确定保存该设置?</div>-->
<!--      <span slot="footer" class="dialog-footer">-->
<!--          <el-button class="border-btn" @click="dialogVisible = false">取 消</el-button>-->
<!--          <el-button class="main-btn" type="primary" @click="determine">确 定</el-button>-->
<!--        </span>-->
<!--    </el-dialog>-->
  </div>
</template>
<script>
  import {setSystemSetting,setPrintSetting} from "@/utils/localStorageData";
  import Templates from "../index";
  import {getSystemInfo,saveSystemInfo} from "@/api/clinicManagement";
  export default {
    name: "systemSetting",
    components: {Templates},
    data() {
      return {
        dialogVisible:false,
        activeName: 'first',
        currentPage:100,
        isRadio:'1',
        tableData:[
          // {
          //   module:'模块管理',
          //   setting:'1'
          // },
          // {
          //   module:'药品管理',
          //   setting:'2'
          // },
          // {
          //   module:'收药/发药管理',
          //   setting:'1'
          // },
        ],
        options: [{
          value: '选项1',
          label: '58mm'
        }, {
          value: '选项2',
          label: '80mm'
        },],
        ruleForm:{
          size:'',
          look:'',
          lookc:'',
          footer:'',
          des:'',
          direction:'1',
          price:'',
          total:'',
        },
        id:'',
        dic_key:'',
        rules:{

        }
      };
    },
    created() {
      //获取系统信息
      this.getSystemInfo();
    },
    methods:{
      //获取系统信息
      getSystemInfo(){
        this.tableData=[]
        if(this.activeName=='first'){
          getSystemInfo({type:1,key:'function_setting'}).then(res=>{
            if(res.code==200){
              this.id = res.data.id
              this.dic_key = res.data.dic_key
              // console.log(JSON.parse(res.data.dic_value))
              let objNew = []
              for(let i in JSON.parse(res.data.dic_value)){
                objNew.push({[i]:JSON.parse(res.data.dic_value)[i]})
              }
              this.tableData.push({module:objNew[0].charge_dispensing_management.model_name,setting:objNew[0].charge_dispensing_management.function_set})
              this.tableData.push({module:objNew[1].drugs_management.model_name,setting:objNew[1].drugs_management.function_set})
              this.tableData.push({module:objNew[2].registration_management.model_name,setting:objNew[2].registration_management.function_set})
              console.log(this.tableData)
            }
          })
        }else{
          getSystemInfo({type:2,key:'print_setting'}).then(res=>{
            if(res.code==200){
              this.id = res.data.id
              this.dic_key = res.data.dic_key
              let objNew = []
              for(let i in JSON.parse(res.data.dic_value)){
                objNew.push({[i]:JSON.parse(res.data.dic_value)[i]})
              }
              this.ruleForm.size = objNew[1].receipt_set.paper
              this.ruleForm.look = objNew[1].receipt_set.rise
              this.ruleForm.footer = objNew[1].receipt_set.inscribe
              this.ruleForm.des = objNew[1].receipt_set.print_detail
              console.log(this.ruleForm.des)
              this.ruleForm.lookc = objNew[0].prescription_set.rise
              this.ruleForm.direction = String(objNew[0].prescription_set.prescription_direction)
              this.ruleForm.total = objNew[0].prescription_set.print_total_price
              this.ruleForm.price = objNew[0].prescription_set.print_uint_price
            }
          })
        }
      },
      //保存设置
      saveSetting(){
        // this.dialogVisible = true
        this.$confirm('确定保存该设置?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.determine()
        }).catch(() => {

        });
      },
      //确定
      determine(){
        console.log(this.tableData)
        let charge_dispensing_management = {} , drugs_management = {}, registration_management = {}
        this.tableData.forEach(item=>{
          console.log(item)
          if(item.module=='收费/发药管理'){
            charge_dispensing_management.function_set = item.setting
            charge_dispensing_management.model_name = item.module
          }else if(item.module=='药品管理'){
            drugs_management.function_set = item.setting
            drugs_management.model_name = item.module
          }else if(item.module=='挂号管理'){
            registration_management.function_set = item.setting
            registration_management.model_name = item.module
          }
        })
        let dic_value = {}
        dic_value.charge_dispensing_management=charge_dispensing_management
        dic_value.drugs_management=drugs_management
        dic_value.registration_management=registration_management
        let data={
          id:this.id,
          dic_key:this.dic_key,
          dic_value:JSON.stringify(dic_value)
        }
        saveSystemInfo(this.id,data).then(res=>{
          if(res.code==200){
            this.$store.dispatch("setting/setSystemSetting");
              this.$store.dispatch("user/getInfo").then(()=>{
                  window.location.reload();
              });
          }
        })
      },
      //保存打印
      savePrint(){
        let receipt_set = {} ,prescription_set = {}
        receipt_set.paper = this.ruleForm.size
        receipt_set.rise = this.ruleForm.look
        receipt_set.inscribe = this.ruleForm.footer
        receipt_set.print_detail = this.ruleForm.des
        prescription_set.rise = this.ruleForm.lookc
        prescription_set.prescription_direction = Number(this.ruleForm.direction)
        prescription_set.print_total_price = this.ruleForm.total
        prescription_set.print_uint_price = this.ruleForm.price
        let dic_value = {}
        dic_value.prescription_set=prescription_set
        dic_value.receipt_set=receipt_set
        let data={
          id:this.id,
          dic_key:this.dic_key,
          dic_value:JSON.stringify(dic_value)
        }
        saveSystemInfo(this.id,data).then(res=>{
          if(res.code==200){
            this.$store.dispatch("setting/setPrintSetting")
              this.$store.dispatch("user/getInfo").then(()=>{
                  window.location.reload();
              });
            this.$message.success('保存成功')
          }
        })
      },
      //切换
      handleClick(){
        this.getSystemInfo()
      },
      //监听条数变化
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      //监听页码变化
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
      },
    }
  };
</script>
<style lang="scss" scoped>
  @import "@/assets/scss/clinicManagement/modules/systemSetting.scss";
  @import "@/assets/scss/drugsManagement/pushWarehouse.scss";
</style>
